package com.qianbei.user.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.util.ConstantValue;
import com.qianbei.common.view.SideBar;

import org.json.JSONException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 国家代码
 *
 * jiaowenzheng
 *
 * Created by user on 16/10/13.
 */

public class CountryCodeActivity extends BaseActivity implements SideBar.OnTouchingLetterChangedListener{

    private String[] tag = {"热门","A","B","C","D","E","F","G","H","I","G","K","L","M","N","O","P",
            "Q","R","S","T","U","V","W","X","Y","Z"};

    private List<CountryCodeModel> mCountryCodeList;

    private ListView mListView;
    private SideBar mSideBar;
    private TextView mChatTipTv;

    private CountryCodeAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_country_code);

        setTitle(R.string.country_code);

        loadData();

        mChatTipTv = (TextView) findViewById(R.id.tv_char_tip);
        mSideBar = (SideBar) findViewById(R.id.side_bar);
        mSideBar.setTextView(mChatTipTv);
        mSideBar.setOnTouchingLetterChangedListener(this);
        mListView = (ListView) findViewById(R.id.country_code_list_view);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                CountryCodeModel model = mAdapter.getItem(position);

                Intent intent = new Intent();
                intent.putExtra(ConstantValue.COUNTRY_NAME_KEY,model.getCountry());
                intent.putExtra(ConstantValue.COUNTRY_CODE_KEY,model.getCode());
                setResult(RESULT_OK,intent);
                finish();
            }
        });

        mAdapter = new CountryCodeAdapter(this,mCountryCodeList,R.layout.item_county_code);
        mListView.setAdapter(mAdapter);
    }

    public void loadData(){

        mCountryCodeList = new ArrayList<>();
        String json = getFromAssets("local_country");
        if (TextUtils.isEmpty(json)){
            return;
        }

        try {
            org.json.JSONObject jsonObject = new org.json.JSONObject(json);

            for (int i = 0;i < tag.length;i++){

                if (jsonObject.has(tag[i])){
                    List<CountryCodeModel> modelList = JSON.parseArray(jsonObject.getString(tag[i]),CountryCodeModel.class);
                    for (CountryCodeModel item : modelList){
                        item.setPinYin(tag[i]);
                    }
                    mCountryCodeList.addAll(modelList);
                }
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }



    }



    public String getFromAssets(String fileName){
        try {
            InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line="";
            String Result="";
            while((line = bufReader.readLine()) != null){
                Result += line;
            }

            return Result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onTouchingLetterChanged(String s) {

        int position = mAdapter.getPositionForSection(s);
        if(position != -1){
            mListView.setSelection(position);
        }
    }

}
